<?php

namespace app\admin\controller;

use app\admin\model\Meun as MeunModal;
use app\admin\model\Role as RoleModal;
use app\admin\validate\Route as RouteValidate;
use app\BaseController;
use app\Request;
use think\facade\Request as faRequest;

class Route extends BaseController
{
    protected $middleware = ['Auth'];
    private $childerKey = ['name', 'enName', 'icon', 'keepAlive', 'showNext'];

    public function list(Request $request) {
        $user = $request->user;
        $sql = new MeunModal();
        $roleSql = new RoleModal();
        $info = $roleSql->find($user['mentId']);
        $oneList = $sql->where('parentId', 0)->where('menuType', 0)->where('id', 'in', $info['menu_ids'])->order('sortOrder', 'desc')->select()->toArray();
        $otherList = $sql->where('parentId', '<>',0)->where('menuType', 0)->where('id', 'in', $info['menu_ids'])->order('sortOrder', 'desc')->select()->toArray();

        foreach ($otherList as $key => $item) {
            foreach ($this->childerKey as $label) {
                $item['meta'][$label] = $item[$label];
            }
            $item['meta']['title'] = $item['name'];
            $item['meta']['name'] = $item['enName'];
            $item['children'] = [];
            $otherList[$key] = $item;
        }
        foreach ($oneList as $key => $item) {
            foreach ($this->childerKey as $label) {
                $item['meta'][$label] = $item[$label];
            }
            $item['meta']['title'] = $item['name'];
            $item['meta']['name'] = $item['enName'];
            $item['children'] = $this->loopChilder($item['id'], $otherList);
            $oneList[$key] = $item;
        }
        return result()->success($oneList);
    }

    private function loopChilder($parentId, $allList) {
        $list = [];
        foreach ($allList as $item) {
            if($item['parentId'] == $parentId) {
                $item['children'] = $this->loopChilder($item['id'], $allList);
                $list[] = $item;
            }
        }
        return $list;
    }

    public function detail() {
        $params = faRequest::post(['name']);
        $sql = new MeunModal();
        $info = $sql->where('name', $params['name'])->find();
        unset($info->component);
        unset($info->create_at);
        unset($info->metaIcon);
        unset($info->metaTitle);
        unset($info->parentId);
        unset($info->path);
        unset($info->sort);
        unset($info->update_at);
        return result()->success($info);
    }

    public function allList() {
        $sql = new MeunModal();
        $oneList = $sql->where('parentId', 0)->where('menuType', 0)->order('sortOrder', 'desc')->select()->toArray();
        $otherList = $sql->where('parentId', '<>',0)->where('menuType', 0)->order('sortOrder', 'desc')->select()->toArray();
        foreach ($otherList as $key => $item) {
            foreach ($this->childerKey as $label) {
                $item['meta'][$label] = $item[$label];
            }
            $item['meta']['title'] = $item['name'];
            $item['meta']['name'] = $item['enName'];
            $item['children'] = [];
            $otherList[$key] = $item;
        }
        foreach ($oneList as $key => $item) {
            foreach ($this->childerKey as $label) {
                $item['meta'][$label] = $item[$label];
            }
            $item['meta']['title'] = $item['name'];
            $item['meta']['name'] = $item['enName'];
            $item['children'] = $this->loopChilder($item['id'], $otherList);
            $oneList[$key] = $item;
        }
        return result()->success($oneList);
    }

    public function loopAllGlobal($list) {
        foreach ($list as $item) {
            foreach ($this->childerKey as $label) {
                $item['meta'][$label] = $item[$label];
                $item['meta']['name'] = $item['enName'];
            }
            $item['meta']['title'] = $item['name'];
            $item['children'] = $item->children;
            $this->loopAllGlobal($item['children']);
        }
        return $list;
    }

    public function add() {
        $params = faRequest::post(['sortOrder', 'parentId', 'menuType', 'name', 'enName', 'path', 'component', 'permission', 'keepAlive', 'showNext', 'icon']);
        $validate = new RouteValidate();
        $sceneName = $params['menuType'] == 0 ? 'add' : 'addBtn';
        if (!$validate->scene($sceneName)->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        if(empty($params['sortOrder'])) {
            $params['sortOrder'] = 1;
        }
        $sql = new MeunModal();
        $sql->save($params);
        return result()->success("添加成功");
    }

    public function edit() {
        $params = faRequest::post(['id', 'sortOrder', 'parentId', 'menuType', 'name', 'enName', 'path', 'component', 'permission', 'keepAlive', 'showNext', 'icon']);
        $validate = new RouteValidate();
        if (!$validate->scene('edit')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $sql = new MeunModal();
        $info = $sql->find($params['id']);
        $info->save($params);
        return result()->success("修改成功");
    }

    public function sort() {
        $params = faRequest::post(['id', 'sortOrder']);
        $validate = new RouteValidate();
        if (!$validate->scene('sort')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $sql = new MeunModal();
        $info = $sql->find($params['id']);
        $info->save($params);
        return result()->success("修改成功");
    }

    public function dels() {
        $params = faRequest::post(['ids']);
        $validate = new RouteValidate();
        if (!$validate->scene('dels')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $sql = new MeunModal();
        $sql->destroy($params['ids']);
        return result()->success("删除成功");
    }
}